package com.brillilab.starter.controller.aio.kit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.enums.inventory.InventoryRecordEnum;
import com.brillilab.domain.pa.kit.ReagentAddMorePa;
import com.brillilab.domain.pa.kit.ReagentAntibodyPa;
import com.brillilab.domain.pa.kit.ReagentCurrencyPa;
import com.brillilab.domain.pa.kit.ReagentPlasmidPa;
import com.brillilab.domain.pa.kit.ReagentPlasmidTemplatePa;
import com.brillilab.domain.pa.kit.ReagentPrimerPa;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.inventory.IInventoryRecordService;
import com.brillilab.service.core.kit.IReagentPlasmidService;
import com.brillilab.service.logic.kit.ReagentLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;

/**
 * 新建试剂
 * 
 * @author lj
 *
 */
@RestController
@RequestMapping("/api/reagent/add")
public class ReagentAddController {
	@Autowired
	private ReagentLogic reagentLogic;
	@Autowired
	private UsersLogic usersLogic;
	@Autowired
	private IReagentPlasmidService reagentPlasmidService;
	@Autowired
	private IInventoryRecordService inventoryRecordService;
	
	/**
	 * 新增通用类型试剂
	 * 
	 * @return
	 */
	@AspectLog
	@RequestMapping(value = "/currency", method = RequestMethod.POST)
	public ResponseVo addCurrency(@RequestHeader(name=StarterConstant.TOKEN) String token,@RequestBody ReagentCurrencyPa pa) {
		UserInfoVo urs=usersLogic.getUserByToken(token);
		pa.setLabId(urs.getLabId());
		pa.setCreatorId(urs.getLabMemberId());
		Long kitId = reagentLogic.addCurrencyReagent(pa);
		inventoryRecordService.add(urs, kitId, InventoryRecordEnum.CREATE);
		return ResponseVo.successMap("kitId",kitId);
	}

	/**
	 * 新增抗体类型试剂
	 * 
	 * @return
	 */
	@AspectLog
	@RequestMapping(value = "/antibody", method = RequestMethod.POST)
	public ResponseVo addAntibody(@RequestHeader(name=StarterConstant.TOKEN) String token,@RequestBody ReagentAntibodyPa pa) {
		UserInfoVo urs=usersLogic.getUserByToken(token);
		pa.getReagentCurrencyPa().setLabId(urs.getLabId());
		pa.getReagentCurrencyPa().setCreatorId(urs.getLabMemberId());
		Long kitId = reagentLogic.addAntibodyReagent(pa);
		inventoryRecordService.add(urs, kitId, InventoryRecordEnum.CREATE);
		return ResponseVo.successMap("kitId",kitId);
	}

	/**
	 * 新增引物类型试剂
	 * 
	 * @return
	 */
	@AspectLog
	@RequestMapping(value = "/primer", method = RequestMethod.POST)
	public ResponseVo addPrimer(@RequestHeader(name=StarterConstant.TOKEN) String token,@RequestBody ReagentPrimerPa pa) {
		UserInfoVo urs=usersLogic.getUserByToken(token);
		pa.getReagentCurrencyPa().setLabId(urs.getLabId());
		pa.getReagentCurrencyPa().setCreatorId(urs.getLabMemberId());
		Long kitId = reagentLogic.addPrimerReagent(pa);
		inventoryRecordService.add(urs, kitId, InventoryRecordEnum.CREATE);
		return ResponseVo.successMap("kitId",kitId);
	}

	/**
	 * 新增质粒类型试剂
	 * 
	 * @return
	 */
	@AspectLog
	@RequestMapping(value = "/plasmid", method = RequestMethod.POST)
	public ResponseVo addPlasmid(@RequestHeader(name=StarterConstant.TOKEN) String token,@RequestBody ReagentPlasmidPa pa) {
		UserInfoVo urs=usersLogic.getUserByToken(token);
		pa.getReagentCurrencyPa().setLabId(urs.getLabId());
		pa.getReagentCurrencyPa().setCreatorId(urs.getLabMemberId());
		Long kitId = reagentLogic.addPlasmidReagent(pa);
		inventoryRecordService.add(urs, kitId, InventoryRecordEnum.CREATE);
		return ResponseVo.successMap("kitId",kitId);
	}

	/**
	 * 新增多成分试剂
	 * 
	 * @return
	 */
	@AspectLog
	@RequestMapping(value = "/more", method = RequestMethod.POST)
	public ResponseVo addMore(@RequestHeader(name=StarterConstant.TOKEN) String token, @RequestBody ReagentAddMorePa pa) {
		UserInfoVo urs=usersLogic.getUserByToken(token);
		pa.getReagentCurrencyPa().setLabId(urs.getLabId());	
		pa.getReagentCurrencyPa().setCreatorId(urs.getLabMemberId());
		
		Long kitId = reagentLogic.addMoreReagent(pa);
		inventoryRecordService.add(urs, kitId, InventoryRecordEnum.CREATE);
		return ResponseVo.successMap("kitId",kitId);
	}

	/**
	 * 获取质粒模版分页
	 * @param pa
	 * @return
	 */
	@RequestMapping(value = "/plasmid/templateList", method = RequestMethod.POST)
	public ResponseVo getPlasmidTemplateList(@RequestHeader(name=StarterConstant.TOKEN) String token,@RequestBody ReagentPlasmidTemplatePa pa) {
		UserInfoVo urs=usersLogic.getUserByToken(token);
		pa.setLabId(urs.getLabId());
		return ResponseVo.success(reagentPlasmidService.selectTemplatePage(pa));
	}
	
	/**
	 * 获取质粒模版信息
	 * @param plasmidTemplateId
	 * @return
	 */
	@RequestMapping(value = "/plasmid/templateInfo", method = RequestMethod.GET)
	public ResponseVo getPlasmidTemplateInfo(@RequestParam Long plasmidTemplateId) {
		return ResponseVo.success(reagentPlasmidService.selectInfo(plasmidTemplateId));
	}
	
}
